﻿using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Logging;
using Sino.CapacityCloud.Infrastructure.Events;
using Sino.CapacityCloud.Infrastructure.Services;
using Sino.CapacityCloud.Repositories.Models;
using Sino.CapacityCloud.Services;
using System;
using System.Threading.Tasks;

namespace Sino.CapacityCloud.Tests.Mocks
{

    public class FakeTmsEventBusSource
        : BackgroundWorker, IFakeTmsEventBusSource
    {

        private readonly IDistributedCache _cache;
        private readonly ITmsServiceBus _tmsServiceBus;
        private readonly IEventBus _eventBus;
        private readonly Random _random;

        public ApplicationDbContext DataContext { get; }

        public FakeTmsEventBusSource(
            ILoggerFactory loggerFactory,
            ApplicationDbContext dbContext,
            IDistributedCache cache,
            ITmsServiceBus tmsServiceBus,
            IEventBus eventBus)
            : base(loggerFactory)
        {
            this._cache = cache;
            this._tmsServiceBus = tmsServiceBus;
            this._eventBus = eventBus;
            this._random = new Random((int)DateTime.Now.Ticks);

            this.DataContext = dbContext;
            this.Start();
        }

        public override void Start()
        {
            base.Start();
            this._eventBus.Register(this);
        }

        protected async override Task<bool> Process()
        {
            var function = this._random.Next(1, 5);
            switch (function)
            {
                case 1:
                    break;
                case 2:
                    break;
                case 3:
                    break;
                case 4:
                    break;
                default:
                    break;
            }
            await Task.Delay(TimeSpan.FromMinutes(function));
            return await Task.FromResult(true);
        }

    }

}